package class08;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangchaoliang
 * create 2022
 */
public class MaxHappy3 {

    public static class Employee{
        public int happy;
        public List<Employee> nexts;

        public Employee(int h){
            happy = h;
            nexts = new ArrayList<>();
        }
    }

    public static class Info{
        public int no;
        public int yes;

        public Info(int n,int y){
            no = n;
            yes = y;
        }
    }

    public static Info process(Employee x){
        if (x==null)
            return new Info(0,0);
        int no = 0;
        int yes = x.happy;
        for (Employee next:x.nexts){
            Info nextInfo = process(next);
            no += Math.max(nextInfo.no,nextInfo.yes);
            yes += nextInfo.no;
        }
        return new Info(no,yes);
    }

    public static int maxHappy(Employee head){
        Info allInfo = process(head);
        return Math.max(allInfo.no,allInfo.yes);
    }
}
